javascriptfandomcom-20200222-history
Number
A number is , and can be an , an , or one of the special values Infinity, -Infinity, and NaN. Literal Syntax Definition Numbers have several syntaxes, for convenience. Decimal A decimal number literal consists of a number of digits, with an optional decimal point and decimal part. 42 2.3 17.24 Scientific For very large or very small numbers, it can often be useful to represent them in 'scientific notation', defined as m(10 ^ n) . In Javascript, this is written as m''e''n or m''E''n, where m'' and ''n are signed decimal integers. 5e3 // 5000 5e-3 // 0.005 Hexadecimal It's sometimes handy to represent numbers in hexadecimal. In Javascript, hexadecimal numbers are expressed as a string of digits from 0 to F with a prefix of 0x or 0X. The case used for the alphabetical digits is unimportant. 0x5 // 5 0xF // 15 0xff // 255 0xDEADBEEF // 3735928559 Operators These operators can be used on numbers. Arithmetic Operators These operators perform basic arithmetic with two numbers. Binary + (Addition) Adds its two operands together. Beware: if either operand is a string this is in fact the concatenation operator. 3 + 5 // 8 2 + 1.9 // 3.9 Binary - (Subtraction) Subtracts the right operand from the left operand. 5 - 3 // 5 2 - 1.9 // 0.1 1 - 2 // -1 * (Multiplication) Multiplies two numbers together. 2 * 5 // 10 5 * 1.5 // 7.5 / (Division) Divides the left operand by the right operand. 10 / 5 // 2 7.5 / 1.5 // 5 5 / 2 // 2.5 % (Remainder) Divides the left operand by the right operand, and returns the remainder. For floats, uses the truncated form: for all floats a and integers b, a % b is equivalent to (a - Math.floor(a)) + Math.floor(a) % b. 5 % 2 // 1 4 % 2 // 0 5.5 % 2 // 1.5 Transformation Operators Unary - (Negation) Returns the negative equivalent of the number (i.e. the number subtracted from zero). -5 // -5 -10 // -10 -(5 / 2) // -2.5 Unary + (Identity) Returns the number. Mostly useful for coercing values of other types to number. +42 // 42 +"42" // 42 Comparison Operators Comparison operators take two numbers and return a boolean value. Any comparison containing NaN will always return false. NaN is therefore the only value unequal to itself, which provides an easy way to check for it (however, isNaN() will get the job done, too). (Equals) Compares two values for equality. Values of different types are automatically considered unequal. 2 + 2 5 // false 2 + 2 4 // true ! (Not Equals) Compares two values for inequality; x ! y is equivalent to !(x y). 2 + 2 ! 5 // true > (Greater Than) Compares two values and returns true if the first is greater than the second. 2 > 1 // true 2 > 1.5 // true 2 > 2 // false 2 > 3 // false < (Less Than) Less-than. Opposite of greater-than. a < b is equivalent to b > a. 1 < 2 // true >= (Greater Than Or Equal To) Similar to greater-than, but also returns true if the values are equal: a >= b is equivalent to a > b || a b. 2 >= 1 // true 2 >= 2 // true 2 >= 3 // false <= (Less Than Or Equal To) The opposite of >=. a <= b is equivalent to a < b || a b. 2 <= 1 // false 2 <= 2 // true 2 <= 3 // true Bitwise Operators These operators treat all numbers as thirty-two-bit binary values, and apply various binary operations to them. ~ (Bitwise Not) Converts each 1 in the number to a 0, and each 0 to a 1. In these examples, the suffixes 'bin' and 'dec' will be used to indicate that the number is binary or decimal, respectively, where confusing. ~9 // -10 ~-9 // 8 & (Bitwise AND) Returns a 1 for each position at which both operands are 1. 4 & 6 // 100 bin // & 110 bin // = 100 bin // = 4 dec | (Bitwise OR) Returns a 1 for each position at which either operand is 1. 4 | 6 // 100 bin // | 110 bin // = 110 bin // = 6 dec ^ (Bitwise XOR) Returns a 1 for each position at which exactly one of the operands is 1. 4 ^ 6 // 100 bin // ^ 110 bin // = 010 bin // = 2 dec << (Bitwise Left-Shift) Shifts the left operand the number of bits to the left specified by the right operand, filling the gap with zeroes. 7 << 2 // 28 >> (Bitwise Sign-Preserving Right-Shift) Shifts the left operand the number of bits to the right specified by the right operand, filling the gap with ones if the number is negative, or zeroes otherwise. Reverses a left-shift. 28 >> 2 // 7 -28 >> 2 // -7 >>> (Bitwise Right-Shift) Like >>, but always fills with zeroes. 28 >>> 2 // 7 -28 >>> 2 // 1073741817 Category:Literal types